using UnityEngine;
using System.Collections;

public class UI_TextMessage : MonoBehaviour {

	public string id;
	public GetText text;
	public int max = 29;
	private string textReturn;
	
	void Start () 
	{
		if(!string.IsNullOrEmpty(id))
		{
			text = GetText.getInstance();
			textReturn = text.returnText(id);
			breakText();
			
			if(gameObject.GetComponent<TextMesh>())
			{
				gameObject.GetComponent<TextMesh>().text = textReturn;
			}
		}
	}
	
	public void reloadText()
	{
		text = GetText.getInstance();
		textReturn = text.returnText(id);
		breakText();
		
		if(gameObject.GetComponent<TextMesh>())
		{
			gameObject.GetComponent<TextMesh>().text = textReturn;
		}
	}
	
	void breakText()
	{
		string[] newText = textReturn.Split(' ');
		int length = 0;
		string finalString = "";
		
		foreach(string text in newText)
		{
			length += text.ToCharArray().Length;
			
			if(length > max)
			{
				finalString += "\n"+text + " ";
				length = text.ToCharArray().Length;
			}
			else
			{
				finalString += text + " ";
			}			
		}
		
		textReturn = finalString;
		
	}
	
	public void addId(string id)
	{
		this.id = id;
		text = GetText.getInstance();
		textReturn = text.returnText(id);
		breakText();
		
		if(gameObject.GetComponent<TextMesh>())
		{
			gameObject.GetComponent<TextMesh>().text = textReturn;
		}
	}


}
